package com.ebei.admin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ebei.admin.common.util.CommonUtil;
import com.ebei.admin.model.dto.OrgDTO;
import com.ebei.admin.service.SysOrgService;
import com.ebei.admin.service.SysParkService;
import com.ebei.admin.service.impl.SysParkServiceImpl;
import com.ebei.common.constant.CommonConstant;
import com.ebei.common.util.Query;
import com.ebei.common.util.Result;
import com.ebei.common.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("/park")
@RestController
@Api(value = "园区", description = "暂时不用!!!园区接口")
public class ParkController extends BaseController {

    @Autowired
    private SysParkService parkService;
    @Autowired
    private SysOrgService sysOrgService;

    /**
     * 分页查询用户
     * @param params 参数集
     * @return 用户集合
     */
    @ApiOperation(value = "园区分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parkName", value = "园区名称", required = false),
            @ApiImplicitParam(name = "parkSeq", value = "园区编码", required = false)
    })
    @GetMapping("/page")
    private Result<Page> byPage(@RequestParam Map<String, Object> params) {
        CommonUtil.removeT(params);
        Page page = parkService.findByParam(new Query<>(params));
        return new Result<>(page, Result.SUCCESS, "获取成功");
    }

    /**
     * 更新园区信息
     *
     * @param map 园区信息
     * @return R
     */
    @ApiOperation(value = "修改园区", notes = "更新园区的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "园区的组织架构Id", required = true),
            @ApiImplicitParam(name = "parkSeq", value = "园区序列号", required = false),
            @ApiImplicitParam(name = "name", value = "园区名称", required = false),
            @ApiImplicitParam(name = "detail", value = "园区描述", required = false)
    })
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody Map<String, Object> map) throws Exception {
        return parkService.update(map);
    }

    /**
     * 获取所属企业
     *
     * @param map 园区信息
     * @return Result
     */
    @ApiOperation(value = "根据orgId查询企业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "园区id", required = true)/*,
            @ApiImplicitParam(name = "type", value = "组织类型", required = true, dataType = "String", paramType = "RequestParam")*/
    })
    @GetMapping({"/getUnit", "/getCompany"})
    public Result getUnit(@RequestParam Map<String, Object> map) {
        map.put("type", CommonConstant.ORGTYPE_COMP);
        return parkService.findUnitByType(map);
    }

    /**
     * 根据用户id查询园区
     *
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户id查询所属园区")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    @GetMapping("/byUserId")
    public Result findByUserId(@RequestParam Integer userId) {
        return parkService.findByUserId(userId);
    }

    @ApiOperation(value = "查询所有园区")
    @GetMapping("/all")
    public Result findAll() {
        EntityWrapper ew = new EntityWrapper();
        ew.eq("del_flag", CommonConstant.STATUS_NORMAL);
        ew.eq("type", CommonConstant.ORGTYPE_SPARK);
        return new Result(sysOrgService.selectList(ew), Result.SUCCESS);
    }

    /**
     * 根据公司orgId查询所属园区
     *
     * @param map
     * @return
     */
    @ApiOperation(value = "根据公司的orgId查询所属园区")
    @ApiImplicitParam(name = "orgId", value = "公司orgId", required = true)
    @GetMapping({"/byCompanyId", "/byOrgId"})
    public Result byCompanyId(@RequestParam Map<String, Object> map) {
        map.put("type", CommonConstant.ORGTYPE_SPARK);
        return parkService.findUnitByType(map);
    }

    @ApiOperation(value = "禁用/启用园区")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "组织id", required = true),
            @ApiImplicitParam(name = "delFlag", value = "组织类型", required = true)
    })
    @PostMapping("/lock")
    public Result lock(@RequestBody OrgDTO orgDTO) {
        return parkService.lock(orgDTO);
    }

    @ApiOperation(value = "查询园区列表", notes = "根据当前登陆人，显示不同的园区列表，用于企业页面的园区下拉框")
    @GetMapping("/getParkList")
    public Result getParkList(){
        return  parkService.getParkList();
    }
}
